-- Leviathan (c) v.v.balashoff
local Log = require('leviathan/Log')

local settings = {}

settings.filename = 'settings.lua'

function settings:GetFullFilename()
    return love.filesystem.getSaveDirectory()..'/'..settings.filename
end

function settings:IsExists()
    -- check save folder exists
    local path = love.filesystem.getSaveDirectory()
    if not path then
        Log:error('Can not find save path!')
        os.exit()
    end
    if love.filesystem.getInfo(settings.filename) then
        return true
    else
        return false
    end
end

function settings:Load()
    local chunk = love.filesystem.load(settings.filename) -- load the chunk
    local result = chunk() -- execute the chunk
end

function settings:Save()
end

function settings:SaveDefaults()
    local data = ''
    data = data.."Config.ShowIntro = true"..'\n'
    data = data.."Config.Locale = 'en'"..'\n'
    data = data.."Config.ShowFPS = true"..'\n'
    data = data.."Config.PlayMusic = true"..'\n'
    data = data.."Config.PlaySounds = true"..'\n'
    love.filesystem.write(settings.filename, data)
end

function settings:LoadSettings()
    -- settings file
    if not settings:IsExists() then
        Log:warning('Can not find settings file: '..settings:GetFullFilename())
        Log:warning('Creating settings file with default values...')
        settings:SaveDefaults()
    end
    if settings:IsExists() then
        Log:info('Found settings file: '..settings:GetFullFilename())
        Log:info('Loading settings file...')
        settings:Load()
    else
        Log:error('Can not save settings file: '..settings:GetFullFilename())
        os.exit()
    end
end

return settings
